package master.photo.controller

import org.springframework.beans.factory.annotation.Autowired;

import master.photo.domain.Photo 
import master.photo.domain.PhotoAlbum;
import master.photo.domain.Profile 
import master.photo.domain.security.AppUser;
import master.photo.domain.security.UserRole;
import master.photo.service.AppUserService;


class PhotoAlbumController {
	
	@Autowired
	AppUserService appUserService;
	
	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	
	def index = {
		redirect(action: "list", params: params)
	}
	
	def list = {
		def profileId = params.id;
		if (!profileId) {
			AppUser user = appUserService.findByUsername(session['J_USERNAME']);
			UserRole role = user.userRole;
			params.max = Math.min(params.max ? params.int('max') : 10, 100)
			if (role == UserRole.get(1)) {// user role with id = 1 should be administrator
				[photoAlbumInstanceList: PhotoAlbum.list(params),
				photoAlbumInstanceTotal: PhotoAlbum.count(),
				isAdmin: true
				]
			} else {
				def photoAlbums = PhotoAlbum.findAllByProfile(user.profile);
				[photoAlbumInstanceList: photoAlbums, photoAlbumInstanceTotal: photoAlbums.size]
			}
		} else {
			def photoAlbums = PhotoAlbum.findAllByProfile(Profile.get(profileId));
			[photoAlbumInstanceList: photoAlbums, photoAlbumInstanceTotal: photoAlbums.size]
		}
	}
	
	def create = {
		def photoAlbumInstance = new PhotoAlbum()
		photoAlbumInstance.properties = params
		return [photoAlbumInstance: photoAlbumInstance]
	}
	
	def save = {
		def photoAlbumInstance = new PhotoAlbum(params)
		AppUser user = appUserService.findByUsername(session['J_USERNAME']);
		photoAlbumInstance.profile = user.profile;
		
		if (photoAlbumInstance.save(flush: true)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'photoAlbum.label', default: 'PhotoAlbum'), photoAlbumInstance.id])}"
			redirect(action: "show", id: photoAlbumInstance.id)
		}
		else {
			render(view: "create", model: [photoAlbumInstance: photoAlbumInstance])
		}
	}
	
	def show = {
		def photoAlbumInstance = PhotoAlbum.get(params.id)
		if (!photoAlbumInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'photoAlbum.label', default: 'PhotoAlbum'), params.id])}"
			redirect(action: "list")
		} else {
			def photoCriteria = Photo.createCriteria()
			def photoList = photoCriteria {
				eq("photoAlbum", photoAlbumInstance)
				order("dateCreated", "asc")
			}
			[photoAlbumInstance: photoAlbumInstance, photos: photoList]
		}
	}
	
	def edit = {
		def photoAlbumInstance = PhotoAlbum.get(params.id)
		if (!photoAlbumInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'photoAlbum.label', default: 'PhotoAlbum'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [photoAlbumInstance: photoAlbumInstance]
		}
	}
	
	def update = {
		def photoAlbumInstance = PhotoAlbum.get(params.id)
		if (photoAlbumInstance) {
			AppUser user = appUserService.findByUsername(session['J_USERNAME']);
			photoAlbumInstance.profile = user.profile;
			
			if (params.version) {
				def version = params.version.toLong()
				if (photoAlbumInstance.version > version) {
					
					photoAlbumInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'photoAlbum.label', default: 'PhotoAlbum')] as Object[], "Another user has updated this PhotoAlbum while you were editing")
					render(view: "edit", model: [photoAlbumInstance: photoAlbumInstance])
					return
				}
			}
			photoAlbumInstance.properties = params
			if (!photoAlbumInstance.hasErrors() && photoAlbumInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'photoAlbum.label', default: 'PhotoAlbum'), photoAlbumInstance.id])}"
				redirect(action: "show", id: photoAlbumInstance.id)
			}
			else {
				render(view: "edit", model: [photoAlbumInstance: photoAlbumInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'photoAlbum.label', default: 'PhotoAlbum'), params.id])}"
			redirect(action: "list")
		}
	}
	
	def delete = {
		def photoAlbumInstance = PhotoAlbum.get(params.id)
		if (photoAlbumInstance) {
			try {
				photoAlbumInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'photoAlbum.label', default: 'PhotoAlbum'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'photoAlbum.label', default: 'PhotoAlbum'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'photoAlbum.label', default: 'PhotoAlbum'), params.id])}"
			redirect(action: "list")
		}
	}
}
